package com.cg.asm.body.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.cg.asm.body.domain.vo.user.UserGroupRoleVO;
import com.cg.asm.body.domain.vo.user.UserGroupVO;
import com.cg.asm.body.service.user.IUserGroupService;
import com.cg.asm.body.util.constants.DicEnum;
import com.cg.asm.body.util.constants.EnumGroup;
import com.cg.asm.client.CommonUtil;
import com.cg.asm.client.exception.MybatisExample;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user/group")
public class UserGroupController {

    @Resource
    private IUserGroupService userGroupService;

    @ApiOperation(value = "用户分组列表查询方法（分页）", notes = "用户分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页尺寸", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sortBy", value = "排序项", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "descending", value = "正序/倒序", dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "filter", value = "过滤条件", dataType = "String", paramType = "query")
    })
    @GetMapping("/pagedlist")
    public JSONObject pagedlist(String pageIndex, String pageSize, String sortBy, boolean descending, String filter) {
        PageInfo<UserGroupVO> contentsPaginator = userGroupService.selectByUserGroup(MybatisExample.initPagedInfo(
                pageIndex,
                pageSize,
                sortBy,
                descending,
                filter
        ));
        return CommonUtil.successJson(contentsPaginator);
    }

    @ApiOperation(value = "获取系统内置用户分组类别", notes = "用户分组")
    @GetMapping("/userGroupTypeOptions")
    public JSONObject userGroupTypeOptions() {
        return CommonUtil.successJson(DicEnum.getEnum(EnumGroup.USER_GROUP_TYPE));
    }

    @ApiOperation(value = "获取用户分组数据及", notes = "用户分组")
    @GetMapping("/list")
    public JSONObject userGroupList() {
        return CommonUtil.successJson(userGroupService.getUserGroupList());
    }


    @ApiOperation(value = "保存用户分组信息", notes = "用户分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userGroupName", value = "用户分组名称", required = true, dataType = "String", paramType = "body")
    })
    @PostMapping("/saveUserGroup")
    public JSONObject saveUserGroup(String userGroupName) {
        try {
            userGroupService.saveUserGroup(userGroupName);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "更新用户分组信息", notes = "用户分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userGroupId", value = "用户分组Id", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "userGroupName", value = "用户分组名称", required = true, dataType = "String", paramType = "body")
    })
    @PutMapping("/updateUserGroup")
    public JSONObject updateUserGroup(String userGroupId, String userGroupName) {
        try {
            userGroupService.updateUserGroup(userGroupId, userGroupName);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "根据用户分组Id删除分组信息", notes = "用户分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userGroupId", value = "用户分组Id", required = true, dataType = "String", paramType = "body")
    })
    @DeleteMapping("/delUserGroup")
    public JSONObject delUserGroup(@RequestBody String userGroupId) {
        try {
            userGroupService.delUserGroup(userGroupId);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "保存选择的用户分组角色", notes = "用户分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userGroupRoleVO", value = "用户分组角色视图对象", required = true, dataType = "UserGroupRoleVO", paramType = "body")
    })
    @PostMapping("/saveUserGroupRole")
    public JSONObject saveUserGroupRole(@RequestBody UserGroupRoleVO userGroupRoleVO) {
        try {
            userGroupService.saveUserGroupRole(userGroupRoleVO);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

    @ApiOperation(value = "根据用户分组编码查询角色信息", notes = "用户分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userGroupId", value = "用户分组Id", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/roleByUserGroup/{userGroupId}")
    public JSONObject roleByUserGroup(@PathVariable("userGroupId") String userGroupId) {
        List<String> roleIds = userGroupService.roleByUserGroup(userGroupId);
        return CommonUtil.successJson(roleIds);
    }

}
